package com.search.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.search.entity.Search;
import com.search.service.SearchService;
import com.search.util.PageUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/searchtest")
public class SearchController {

	@Resource
	private SearchService s;

	@GetMapping(value = "/search")
	public String search(HttpServletRequest request, Model model, @Valid Search search, BindingResult bindingResult,
			PageUtil page) {
		JSONObject jo = (JSONObject) JSONObject.fromObject(s.search(request, bindingResult, search, page));
		if ((boolean) jo.get("isSuccess")) {
			JSONObject rs = (JSONObject) jo.get("data");
			model.addAttribute("DATA", rs);
			return "searchtest";
		} else {
			model.addAttribute("DATA", jo);
			return "searchError";
		}
	}
	@GetMapping(value = "/search1")
	@ResponseBody
	public String search1(HttpServletRequest request, Model model, @Valid Search search, BindingResult bindingResult,
			PageUtil page) {
		return s.search(request, bindingResult, search, page);
	}

	@GetMapping(value = "/searchArray")
	public String searchArray(Model model) {
		JSONObject jo = (JSONObject) JSONObject.fromObject(s.getSearchArray());
		model.addAttribute("DATA", jo);
		return "searchArray";
	}

}
